/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package questao02;


/**
 *
 * @author SDKW
 */
public class LoggerManager {
    private Logger logger = null;
    private TypeLogger type = null;
    private boolean ativo = false;
    private static LoggerManager loggerManager = null;
    
    public static LoggerManager getInstance(TypeLogger type) {
        if (loggerManager == null) {
            loggerManager = new LoggerManager(type);
        } else {
            loggerManager.updateType(type);
        }
        return loggerManager;
    }
    
    private void updateType(TypeLogger type) {  
        if (logger == null || this.type != type) {        
            if(logger != null) {
                logger.close();
            }

            switch(type) {
                case Database : {
                    logger = new LoggerDatabase();
                } break;

                case File : {
                    logger = new LoggerFile();
                } break;

                case EventViewer :
                default : {
                    type = TypeLogger.EventViewer;
                    logger = new LoggerEventViewer();
                } break;
            }
            this.type = type;
        }
    }
    
    private LoggerManager(TypeLogger type) { 
        updateType(type);
    }
    
    public void log(String log) {
        if (ativo) {
            logger.log(log);            
        }
    }

    /**
     * @return the ativo
     */
    public boolean isAtivo() {
        return ativo;
    }

    /**
     * @param ativo the ativo to set
     */
    public void setAtivo(boolean ativo) {
        this.ativo = ativo;
    }


    
}
